import 'package:flutter/material.dart';
import 'package:flutter_base/base/widget/base_get_builder.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/generated/assets.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_badge_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_growth_path_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_keyword_tag_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_matching_result_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_multi_dimensional_analysis_card.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_quote_image_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_radar_chart_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_recommendation_box.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_text_progress_block.dart';
import 'package:flutter_base/pages/test_detail/widgets/test_visual_meter_block.dart';
import 'package:flutter_base/utils/log/utils_log.dart';
import 'package:flutter_base/utils/test_result_component_util.dart';
import 'package:flutter_base/widgets/appbar/common_app_bar.dart';
import 'package:flutter_base/widgets/button/common_button.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'test_result_logic.dart';

class TestResultPage extends StatelessWidget {
  final logic = Get.put(TestResultLogic());

  TestResultPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<TestResultLogic>(
      backgroundView: Positioned.fill(
          child: Container(
        decoration: const BoxDecoration(

            image: DecorationImage(
                image: AssetImage(
                  Assets.testResultTopBg,
                ),
                fit: BoxFit.fitWidth,
                alignment: Alignment.topCenter)),
      )),
      builder: (controller) {
        return Stack(
          children: [
            Positioned.fill(
                child: SingleChildScrollView(
              controller: controller.scrollController,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.w).copyWith(
                    top: 20.w + kToolbarHeight + ScreenUtil().statusBarHeight,
                    bottom: 60.w + ScreenUtil().bottomBarHeight),
                child: Column(
                  children: [
                    _buildAdvancedReportCard(),
                    SizedBox(height: 20.w),

                    Obx(() {
                      return Column(
                          children:
                              (logic.testDetail.value.result?.components ?? [])
                                  .map((item) {
                        clog('dakdakshd   ${item.toJson()}');
                        return TestResultComponentUtil.buildComponentByType(
                                item)
                            .marginOnly(bottom: 20.w);
                      }).toList());
                    }),

                    // const TestKeywordTagBlock(),
                    // SizedBox(height: 20.w),
                    // TestMatchingResultBlock(testDetail: null),
                    // SizedBox(height: 16.w),
                    // TestRadarChartBlock(testDetail: null),
                    // SizedBox(height: 16.w),
                    // TestMultiDimensionalAnalysisCard(testDetail: null),
                    // SizedBox(height: 16.w),
                    // TestRecommendationBox(testDetail: null),
                    // SizedBox(height: 16.w),
                    // const TestQuoteImageBlock(
                    //     // imageUrl: AppConfig.testImage,
                    //     ),
                    // SizedBox(height: 16.w),
                    // TestBadgeBlock(
                    //   testDetail: null,
                    // ),
                    // SizedBox(height: 16.w),
                    // TestGrowthPathBlock(
                    //   currentStage: 3,
                    //   testDetail: null,
                    //   items: const [
                    //     'Self-awareness',
                    //     'Skill Development',
                    //     'Practical application',
                    //     'Continuous optimization',
                    //     'Influencing Others'
                    //   ],
                    // ),
                    // SizedBox(height: 16.w),
                    // const TextProgressBlock(
                    //   title: 'Summary of psychological analysis',
                    //   subtitle:
                    //       'Analysis and guidance from professional psychologists',
                    //   items: [
                    //     TextProgressItem(
                    //       text:
                    //           'Everyone is unique, and your combination of personality traits makes you who you are today.',
                    //       lineColor: TextProgressColors.blue,
                    //     ),
                    //     TextProgressItem(
                    //       text:
                    //           'Understanding your strengths and room for growth is an important starting point for personal development.',
                    //       lineColor: TextProgressColors.teal,
                    //     ),
                    //     TextProgressItem(
                    //       text:
                    //           'Remember, personality traits are neither good nor bad, the key is how to play to your strengths.',
                    //       lineColor: TextProgressColors.orange,
                    //     ),
                    //     TextProgressItem(
                    //       text:
                    //           'Continuous self-reflection and learning will help you go further on the road of life.',
                    //       lineColor: TextProgressColors.pink,
                    //     ),
                    //     TextProgressItem(
                    //       text:
                    //           'Believe in yourself, embrace change, your potential is far greater than you think.',
                    //       lineColor: TextProgressColors.purple,
                    //     ),
                    //   ],
                    // ),
                    // SizedBox(height: 16.w),
                    // const TestVisualMeterBlock(
                    //   title: 'Comprehensive ability',
                    //   subtitle: 'Comprehensive indicators',
                    //   value: 60,
                    //   level: 'Very high',
                    //   completionRate: 82,
                    // ),
                    // SizedBox(height: 16.w),
                  ],
                ),
              ),
            )),
            Positioned(
                bottom: ScreenUtil().bottomBarHeight,
                // bottom: ScreenUtil()(),
                right: 16.w,
                left: 16.w,
                child: CommonButton.expend(
                  title: 'Test again',
                  onClick: () {
                    // logic.startTest();
                  },
                )),
            Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: Obx(() {
                  return CommonAppBar(
                    title: 'Test Details',
                    leadingImageColor: logic.isDarkTitle.value
                        ? Colors.white
                        : AppColor.color0C0A09,
                    titleColor: logic.isDarkTitle.value
                        ? Colors.white
                        : AppColor.color0C0A09,
                    backgroundColor:
                        Colors.white.withOpacity(logic.appBarOpacity),
                    actions: [
                      GestureDetector(
                        child: Container(
                          width: 32.w,
                          height: 32.w,
                          decoration: const BoxDecoration(
                            color: Colors.white,
                            shape: BoxShape.circle,
                          ),
                          alignment: Alignment.center,
                          child: AutoImage(
                            Assets.commonShare,
                            width: 24.w,
                          ),
                        ),
                      ),
                      16.horizontalSpace,
                    ],
                  );
                }))
          ],
        );
      },
    );
  }

  // 结果头部
  Widget _buildResultHeader(TestResultLogic controller) {
    return Column(
      children: [
        // 成功图标
        Container(
          width: 80.w,
          height: 80.w,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: Color(0xFFEAF9F2),
          ),
          child: Icon(
            Icons.check_circle,
            color: Color(0xFF30C681),
            size: 40.w,
          ),
        ),
        SizedBox(height: 16.w),

        // 标题
        Text(
          '测试完成',
          style: TextStyle(
            fontFamily: 'Outfit',
            fontSize: 24.sp,
            fontWeight: FontWeight.w600,
            color: AppColor.color0C0A09,
          ),
        ),
        SizedBox(height: 8.w),

        // 副标题
        Text(
          '以下是您的测试结果分析',
          style: TextStyle(
            fontFamily: 'Outfit',
            fontSize: 16.sp,
            color: AppColor.color7F909F,
          ),
        ),
      ],
    );
  }

  // 空结果状态
  Widget _buildEmptyResult() {
    return Container(
      height: 300.w,
      alignment: Alignment.center,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          AutoImage(
            Assets.emptyNoData,
            width: 120.w,
            height: 120.w,
          ),
          SizedBox(height: 16.w),
          Text(
            '结果加载中...',
            style: TextStyle(
              fontSize: 16.sp,
              color: AppColor.color7F909F,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAdvancedReportCard() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(24.w),
      ),
      padding: EdgeInsets.all(16.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 高级报告标签
          Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12.w),
              gradient: const LinearGradient(
                colors: [
                  Color(0xFF72FFFD),
                  Color(0xFF69FF7D),
                  Color(0xFFFFADFB),
                ],
              ),
            ),
            padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 4.w),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  'Advanced Report',
                  style: TextStyle(
                    fontFamily: 'Outfit',
                    fontSize: 12.sp,
                    fontWeight: FontWeight.w500,
                    color: const Color(0xFF0C0A09),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 6.w),

          // 标题
          Text(
            'Discover the real you',
            style: TextStyle(
              fontFamily: 'Outfit',
              fontSize: 28.sp,
              fontWeight: FontWeight.w700,
              color: const Color(0xFF0C0A09),
              letterSpacing: -1.5, // 添加负的letterSpacing来减小文字间距
            ),
          ),
          SizedBox(height: 6.w),

          // 描述
          Text(
            'Gain deeper insights into your inner traits through scientific personality assessments',
            style: TextStyle(
              fontFamily: 'Outfit',
              fontSize: 12.sp,
              color: const Color(0xFF7F909F),
            ),
          ),
        ],
      ),
    );
  }
}
